﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerThanhPhanThucPhamDao : IThanhPhanThucPhamDao
    {
        public SqlServerThanhPhanThucPhamDao()
        {
            SQLUtility.TableName = "ThanhPhanThucPham";
        }

        //Trả về đối tượng báo ăn
        private ThanhPhanThucPham MakeThanhPhanThucPham(DataRow row)
        {
             Guid _id=Guid.Parse(row["Id"].ToString());
             int _schoolID = int.Parse(row["SchoolID"].ToString());
             string _tenThanhPhanThucPham = row["TenThanhPhanThucPham"].ToString();
             int _sTT = int.Parse(row["STT"].ToString());
         
            return new ThanhPhanThucPham(_id, _schoolID, _tenThanhPhanThucPham, _sTT);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<ThanhPhanThucPham> MakeThanhPhanThucPhams(DataTable dt)
        {
            IList<ThanhPhanThucPham> list = new List<ThanhPhanThucPham>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakeThanhPhanThucPham(row));
            }
            return list;
        }
        private DbParameter[] Take(ThanhPhanThucPham ThanhPhanThucPham)
        {
            DbParameter[] parameters = new DbParameter[4];
            parameters[0] = new SqlParameter("@Id", ThanhPhanThucPham.Id);
            parameters[1] = new SqlParameter("@SchoolID", ThanhPhanThucPham.SchoolID);
            parameters[2] = new SqlParameter("@TenThanhPhanThucPham", ThanhPhanThucPham.TenThanhPhanThucPham);

            parameters[3] = new SqlParameter("@STT", ThanhPhanThucPham.STT);

            return parameters;
        }


        public IList<Business_Objects.ThanhPhanThucPham> GetThanhPhanThucPham()
        {
            SQLUtility.TableName = "ThanhPhanThucPham";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakeThanhPhanThucPhams(dt);
        }

        public Business_Objects.ThanhPhanThucPham GetThanhPhanThucPhamByID(string ThanhPhanThucPhamID)
        {
            SQLUtility.TableName = "ThanhPhanThucPham";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@Id", ThanhPhanThucPhamID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakeThanhPhanThucPham(row);
            }
        }

        public void InsertThanhPhanThucPham(Business_Objects.ThanhPhanThucPham ThanhPhanThucPham)
        {
            SQLUtility.TableName = "ThanhPhanThucPham";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(ThanhPhanThucPham), true);
        }

        public int UpdateThanhPhanThucPham(Business_Objects.ThanhPhanThucPham ThanhPhanThucPham)
        {
            SQLUtility.TableName = "ThanhPhanThucPham";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(ThanhPhanThucPham)); }
            catch { return -1; }
        }

        public int DeleteThanhPhanThucPham(Business_Objects.ThanhPhanThucPham ThanhPhanThucPham)
        {
            SQLUtility.TableName = "ThanhPhanThucPham";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(ThanhPhanThucPham)); }
            catch { return -1; }
        }
    }
}
